/*
 * button.cpp
 *
 *  Created on: 02.06.2012
 *      Author: Roma Jam
 */

#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "led.h"
#include "button.h"
#include "kl_lib.h"

button_t Btn;

void button_t::Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(BTN_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = BTN1_PIN | BTN2_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(BTN_GPIO, &GPIO_InitStructure);
}

void button_t::Task(void) {
    uint32_t bTmr;
    if (Delay.Elapsed(&bTmr, 200)) {
        if (BTN1_IS_PRESSED() && !Key1WasPressed) {
            Event_Key1();
            Key1WasPressed = true;
        }
        else if (!BTN1_IS_PRESSED() && Key1WasPressed) Key1WasPressed = false;
        if (BTN2_IS_PRESSED() && !Key2WasPressed) {
            Event_Key2();
            Key2WasPressed = true;
        }
        else if (!BTN2_IS_PRESSED() && Key2WasPressed) Key2WasPressed = false;
    }
}

void button_t::Event_Key1(void) {
    LEDG_TOGGLE();
}

void button_t::Event_Key2(void) {
//    LEDB_TOGGLE();
}
